package com.mdoya.controller;

import com.mdoya.domain.ShoppingCart;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.ShoppingCartService;
import com.mdoya.utils.ThreadLocalContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public ResultResponse list() {
        return ResultResponse.success(shoppingCartService.list());
    }

    @PostMapping("/add")
    public ResultResponse add(@RequestBody ShoppingCart shoppingCart) {
        Integer rows = shoppingCartService.add(shoppingCart);
        if (rows > 0) {
            return ResultResponse.success("添加购物车成功");
        }
        return ResultResponse.error("添加购物车失败");
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public ResultResponse cleanShoppingCar() {
        return ResultResponse.success(shoppingCartService.cleanShoppingCar(ThreadLocalContextUtils.getUserLongId()));
    }

    /**
     * 购物车数量处理
     *
     * @return
     */
    @PostMapping("/sub")
    public ResultResponse ShoppingCarCount(@RequestBody Map<String, String> map) {
        log.info("购物车数量处理: {}", map);
        Long userId = ThreadLocalContextUtils.getUserLongId();
        System.err.println("用户 == " + userId);
        return ResultResponse.success(shoppingCartService.ShoppingCarCount(map, userId) > 0 ? "操作成功" : "操作失败");
    }
}
